package com.weipai.demo.service.impl;

import com.weipai.demo.enums.RetCode;
import com.weipai.demo.mapper.CarBrandMapper;
import com.weipai.demo.mapper.CarContentMapper;
import com.weipai.demo.mapper.CarParameterMapper;
import com.weipai.demo.mapper.VehicleModelMapper;
import com.weipai.demo.mapper.base.DCarBrandMapper;
import com.weipai.demo.mapper.base.DCarParameterMapper;
import com.weipai.demo.mapper.base.DVehicleModelMapper;
import com.weipai.demo.model.CarBrand;
import com.weipai.demo.model.CarContent;
import com.weipai.demo.model.VehicleModel;
import com.weipai.demo.service.CarPriceService;
import com.weipai.demo.util.LayuiTablePagingResult;
import com.weipai.demo.util.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/25 16:17
 * @Version 1.0
 */
@Service
public class CarPriceServiceImpl implements CarPriceService {

    private static final Logger log = LoggerFactory.getLogger(CarPriceServiceImpl.class);

    @Autowired
    private DCarBrandMapper dCarBrandMapper;
    @Autowired
    private DCarParameterMapper dCarParameterMapper;
    @Autowired
    private DVehicleModelMapper dVehicleModelMapper;
    @Autowired
    private VehicleModelMapper vehicleModelMapper;
    @Autowired
    private CarParameterMapper carParameterMapper;
    @Autowired
    private CarBrandMapper carBrandMapper;
    @Autowired
    private CarContentMapper carContentMapper;

    //三级列表展示
    @Override
    public Response getCarCategory(Integer sign) {
        Response response = new Response();
        List<Map<String, Object>> carBrandList = this.getCategory(0, 0, sign);
        if (carBrandList == null) {
            response.setCode(RetCode.NO_DATA);
            return response;
        }
        response.setCode(RetCode.SUCCESS);
        response.setData(carBrandList);
        return response;
    }

    //递归展示数据
    private List<Map<String, Object>> getCategory(Integer pid, Integer num, Integer sign) {
        List<Map<String, Object>> carBrand = carBrandMapper.getCarCategory(pid);
        if (carBrand != null) {
            num = num + 1;
            for (int i = 0; i < carBrand.size(); i++) {
                Map<String, Object> carItem = carBrand.get(i);
                carItem.put("layer", num);
                carItem.put("isEdit", false);
                if (num < 3) {
                    carItem.put("children", this.getCategory((int) carItem.get("id"), num, sign));
                }
            }
        }
        if (num < 4 && sign == 1) {
            Map<String, Object> addMore = new HashMap<>();
            addMore.put("pid", pid);
            addMore.put("add", true);
            addMore.put("id", 0);
            addMore.put("brand", "点击添加");
            carBrand.add(addMore);
        }

        return carBrand;
    }

    // 添加或编辑品牌
    @Override
    public Response handleCarCategory(CarBrand carBrand) {
        Response response = new Response();
        Integer rest;
        if (carBrand.getId() != null) { //编辑
            rest = carBrandMapper.updateByPrimaryKey(carBrand);
        } else {
            Integer brandNum = carBrandMapper.getBrandName(carBrand.getBrand());
            if (brandNum > 0) {
                response.setCode(RetCode.IDENTICAL_VEHICLEMODEL);
                return response;
            }
            rest = carBrandMapper.insert(carBrand);
        }
        if (rest == 0) {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //删除分类
    @Override
    public Response delCarCategory(Integer id) {
        Response response = new Response();
        int rest = 0;
        if (id != null) {
            List<Map<String, Object>> carBrand = carBrandMapper.getCarCategory(id);
            if (carBrand.size() > 0) {
                response.setCode(RetCode.DELETE_CHILDREN);
                return response;
            }
            Integer contentNum = carContentMapper.selectCarByPid(id);
            if (contentNum > 0) {
                response.setCode(RetCode.DELETE_CONTENT);
                return response;
            }
            rest = carBrandMapper.deleteById(id);
        }
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        }
        response.setCode(RetCode.REQUEST_ERROR);
        return response;

    }

    // 添加或编辑价格信息
    @Override
    public Response addCarContent(CarContent carContent) {
        Response response = new Response();
        Integer rest;
        if (carContent.getId() != null) { //编辑
            rest = carContentMapper.updateByPrimaryKey(carContent);
        } else {
            Integer brandNum = carContentMapper.getBrandContent(carContent);
            if (brandNum > 0) {
                response.setCode(RetCode.THESAME);
                return response;
            }
            rest = carContentMapper.insert(carContent);
        }
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        }
        response.setCode(RetCode.REQUEST_ERROR);
        return response;
    }

    // 添加或编辑价格信息
    @Override
    public LayuiTablePagingResult getCarInfomationList(Integer bid, Integer tid, Integer pid, Integer page, Integer size) {
        LayuiTablePagingResult result = null;
        List<Map<String, Object>> rest = new ArrayList<>();
        Long getCarsNum;
        Integer start = (page - 1) * size;
        rest = carContentMapper.getCars(bid, tid, pid, start, size);
        getCarsNum = carContentMapper.getCarsNum(bid, tid, pid);
        result = new LayuiTablePagingResult();
        result.setCode(RetCode.SUCCESS.getCode());
        result.setMsg(RetCode.SUCCESS.getMsg());
        result.setCount(getCarsNum);
        result.setData(rest);
        return result;
    }

    // 添加或编辑价格信息
    @Override
    public Response delCarInfomationList(Integer id) {
        Response response = new Response();
        Integer rest = carContentMapper.delById(id);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        }
        response.setCode(RetCode.REQUEST_ERROR);
        return response;
    }


    //车贷信息的添加
    @Override
    public Response save(VehicleModel vehicleModel) {
        Response response = new Response();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String str = format.format(new Date());
        vehicleModel.setCreateTime(new Date());
        int rest = vehicleModelMapper.insert(vehicleModel);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        }
        response.setCode(RetCode.SAVEERROR);
        return response;
    }

    //车贷信息的展示
    @Override
    public Response showApplyfor(Integer page, Integer size) {
        Response response = new Response();
        Integer start = (page - 1) * size;
        List<Map<String, Object>> rest = vehicleModelMapper.getVehicleList(start, size);
        Long restNum = vehicleModelMapper.getVehicleListNum();
        response.setCount(restNum);
        response.setData(rest);
        response.setCode(RetCode.SUCCESS);
        return response;
    }

    //车贷信息的展示
    @Override
    public Response delApplyfor(Integer id) {
        Response response = new Response();

        int rest = vehicleModelMapper.deleteByPrimaryKey(id);
        if (rest > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        }
        response.setCode(RetCode.REQUEST_ERROR);
        return response;
    }


    //==================================================================================================================

    //汽车品牌的修改
    @Override
    public Response update(Integer id, String brand, String type) {
        Response response = new Response();

        CarBrand carBrand = new CarBrand();
        carBrand.setId(id);
        carBrand.setBrand(brand);
        carBrand.setType(type);
        int i = carBrandMapper.updateBrand(carBrand);
        if (i > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }

    }

    //汽车型号的修改
    @Override
    public Response updateModel(Integer id, String vehicleModel, String type) {
        Response response = new Response();
        int i = vehicleModelMapper.updateModel(id, vehicleModel, type);
        if (i > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }

    }


    //车型参数的修改
    @Override
    public Response updateParameter(Integer id, String parameter, String referencePrice, String type) {
        Response response = new Response();
        int i = carParameterMapper.updateParameter(id, parameter, referencePrice, type);
        if (i > 0) {
            response.setCode(RetCode.SUCCESS);
            return response;
        } else {
            response.setCode(RetCode.REQUEST_ERROR);
            return response;
        }
    }


    //==================================================================================================================


    //前端页面车贷信息显示
    @Override
    public Response frontShow(String display, String brand, String vehicleModel, String type) {
        Response response = new Response();
        try {
            if ("1".equals(display) && null == brand && null == vehicleModel) {

                // List<CarBrand> brandList = dCarBrandMapper.select(carBrand);
                List<String> brandList = carBrandMapper.selectdisplay(display, type);
                response.put("brandList", brandList);
                response.setCode(RetCode.SUCCESS);
            }
            if (!"null".equals(brand) && "null".equals(vehicleModel)) {
                List<String> list = carBrandMapper.selectBrand(brand, type);
                response.put("list", list);
                response.setCode(RetCode.SUCCESS);
                return response;
            }
            if (!"null".equals(brand) && !"null".equals(vehicleModel)) {
                List<String> list = carBrandMapper.selectVehicle(brand, vehicleModel, type);
                response.put("list", list);
                response.setCode(RetCode.SUCCESS);
                return response;
            }


        } catch (Exception e) {
            e.printStackTrace();
            log.error("pageShow" + e.getMessage());
            response.setCode(RetCode.RESOURCES_ADD_FAIL);
            return response;
        }
        return response;
    }


    //删除汽车品牌
    @Override
    public Response deleteById(Integer id) {
        Response response = new Response();
        if (id != null) {
            int i = carBrandMapper.deleteById(id);
            if (i > 0) {
                response.setCode(RetCode.SUCCESS);
                return response;
            } else {
                response.setCode(RetCode.REQUEST_ERROR);
                return response;
            }
        }
        return response;
    }


    //==================================================================================================================


    //后台显示车贷信息
 /*   @Override
    public LayuiTablePagingResult pageShow(Integer page, Integer size, String display) {
        LayuiTablePagingResult result= null;
        try {

            CarBrand carBrand=new CarBrand();
            carBrand.setDisplay("1");
            List<CarBrand> brandList = dCarBrandMapper.select(carBrand);



            PageHelper.startPage(page,size);
            //关联c_car_brand,c_car_parameter,c_vehicle_model三张表查询
            List carbarnd = carBrandMapper.selectBydisplay(display);
            //List<JobNews> banners = jobNewsMapper.select(jobNews);

            PageInfo pageInfo=new PageInfo<>(carbarnd);
            result = new LayuiTablePagingResult();
            result.setCode(RetCode.SUCCESS.getCode());
            result.setMsg(RetCode.SUCCESS.getMsg());
            result.setCount(pageInfo.getTotal());
            result.setData(pageInfo.getList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return result;

    }*/


}
